using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using vs.Core;
using vs.Authentication;

namespace Forum
{
    public partial class forum_show : Window
    {
        protected vs.Paging.vsPaging pager;
        private bool isadminforum(string forumID)
        {
            if (Request.IsAuthenticated)
            {
                bool iseditor = Authenticate.IsEditor;
                bool isadmin = Authenticate.IsAdmin;
                bool adminforum = false;
                return (isadmin || adminforum || iseditor);
            }

            return false;
        }

        private int Paging(string forumID)
        {
            int pg = 0;
            if (Request.QueryString["catID"] != null)
                pg = Convert.ToInt32(Request.QueryString["catID"]) - 1;
            engine_forum ef = new engine_forum();

            int pagecount = ef.CountPageForum(forumID);

            if (pagecount > 1)
            {
                string forumdesk = this.GetModuleSetting("forumdesk");
                string urlformat = UrlProvider.DesktopUrl(forumdesk, forumID, "@@page");
                pager.Visible = true;
                pager.showing(pagecount, pg, urlformat);
            }
            return pg;
        }

        private void bind_forum_quick_select(string forumdesk)
        {
            engine_forum engine = new engine_forum();
            DataTable dt = engine.CreateForumTree(forumdesk);
            for (int i = 0; i < dt.Rows.Count; i++)
                ddlForum.Items.Add(new ListItem(dt.Rows[i]["name"].ToString(), dt.Rows[i]["val"].ToString()));
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Page.IsPostBack)
                {
                    
                    engine_xml engine = new engine_xml();
                    string forumID = Request.QueryString["ID"];
                    bool adminforum = isadminforum(forumID);
                    int pg = Paging(forumID);

                    if(forumID != "")
                    {
                        string forumdesk = this.GetModuleSetting("forumdesk");
                        string topicdesk = this.GetModuleSetting("topicdesk");

                        if(topicdesk != "")
                        {
                            XmlDocument doc = engine.ShowForumXML(forumID, pg, adminforum, topicdesk);
                            string skin = ModSettingController.GetModuleSetting("skin", Utils.ModuleID);
                            string template = engine_xml.get_template_folder(skin) + "forum_show.xsl";
                            CommonFunction.XMLTransform(xmlTransformer, template, doc);
                        }
                        
                        if (forumdesk != "")
                            bind_forum_quick_select(forumdesk);

                        lnkAddNew.NavigateUrl = UrlProvider.AdminUrl("fr_topic", "new", forumID);
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

        }

        protected void ddlForum_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlForum.SelectedValue != "")
                Response.Redirect(ddlForum.SelectedValue);
        }
    }
}